home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 276_01 / a16util.c < prev    next >
C/C++ Source or Header  |  1989-10-01  |  14KB  |  504 lines

  1. /*
  2.     HEADER:        CUG276;
  3.     TITLE:        PIC1650 Cross-Assembler (Portable);
  4.     FILENAME:    A16UTIL.C;
  5.     VERSION:    0.0;
  6.     DATE:        06/0151988;
  7.     SEE-ALSO:    A16.H;
  8.     AUTHORS:    William C. Colley III;
  9. */
  10.  
  11. /*
  12.              PIC1650 Cross-Assembler in Portable C
  13.  
  14.          Copyright (c) 1985,1987,1989 William C. Colley, III
  15.  
  16. Revision History:
  17.  
  18. Ver    Date        Description
  19.  
  20. 0.0    JUNE 1989    Derived from version 0.2 of my portable 8085 cross-
  21.             assembler.  WCC3.
  22.  
  23. This module contains the following utility packages:
  24.  
  25.     1)  symbol table building and searching
  26.  
  27.     2)  opcode and operator table searching
  28.  
  29.     3)  listing file output
  30.  
  31.     4)  hex file output
  32.  
  33.     5)  error flagging
  34. */
  35.  
  36. /*  Get global goodies:  */
  37.  
  38. #include "a16.h"
  39.  
  40. /*  Make sure that MSDOS compilers using the large memory model know    */
  41. /*  that calloc() returns pointer to char as an MSDOS far pointer is    */
  42. /*  NOT compatible with the int type as is usually the case.        */
  43.  
  44. char *calloc();
  45.  
  46. /*  Get access to global mailboxes defined in A16.C:            */
  47.  
  48. extern char errcode, line[], title[];
  49. extern int eject, listhex;
  50. extern unsigned address, bytes, errors, listleft, obj[], pagelen;
  51.  
  52. /*  The symbol table is a binary tree of variable-length blocks drawn    */
  53. /*  from the heap with the calloc() function.  The root pointer lives    */
  54. /*  here:                                */
  55.  
  56. static SYMBOL *sroot = NULL;
  57.  
  58. /*  Add new symbol to symbol table.  Returns pointer to symbol even if    */
  59. /*  the symbol already exists.  If there's not enough memory to store    */
  60. /*  the new symbol, a fatal error occurs.                */
  61.  
  62. SYMBOL *new_symbol(nam)
  63. char *nam;
  64. {
  65.     SCRATCH int i;
  66.     SCRATCH SYMBOL **p, *q;
  67.     void fatal_error();
  68.  
  69.     for (p = &sroot; (q = *p) && (i = strcmp(nam,q -> sname)); )
  70.     p = i < 0 ? &(q -> left) : &(q -> right);
  71.     if (!q) {
  72.     if (!(*p = q = (SYMBOL *)calloc(1,sizeof(SYMBOL) + strlen(nam))))
  73.         fatal_error(SYMBOLS);
  74.     strcpy(q -> sname,nam);
  75.     }
  76.     return q;
  77. }
  78.  
  79. /*  Look up symbol in symbol table.  Returns pointer to symbol or NULL    */
  80. /*  if symbol not found.                        */
  81.  
  82. SYMBOL *find_symbol(nam)
  83. char *nam;
  84. {
  85.     SCRATCH int i;
  86.     SCRATCH SYMBOL *p;
  87.  
  88.     for (p = sroot; p && (i = strcmp(nam,p -> sname));
  89.     p = i < 0 ? p -> left : p -> right);
  90.     return p;
  91. }
  92.  
  93. /*  Opcode table search routine.  This routine pats down the opcode    */
  94. /*  table for a given opcode and returns either a pointer to it or    */
  95. /*  NULL if the opcode doesn't exist.                    */
  96.  
  97. OPCODE *find_code(nam)
  98. char *nam;
  99. {
  100.     OPCODE *bsearch();
  101.  
  102.     static OPCODE opctbl[] = {
  103.     { REG_5 + DIR + 1,            0x1c0,    "ADDWF"        },
  104.     { IMMED_8 + 1,                0xe00,    "ANDLW"        },
  105.     { REG_5 + DIR + 1,            0x140,    "ANDWF"        },
  106.     { ADDR_9 + 1,                0xa00,    "B"        },
  107.     { ADDR_9 + 2,                0x603,    "BC"        },
  108.     { REG_5 + BIT_3 + 1,            0x400,    "BCF"        },
  109.     { ADDR_9 + 2,                0x623,    "BDC"        },
  110.     { ADDR_9 + 2,                0x703,    "BNC"        },
  111.     { ADDR_9 + 2,                0x723,    "BNDC"        },
  112.     { ADDR_9 + 2,                0x743,    "BNZ"        },
  113.     { REG_5 + BIT_3 + 1,            0x500,    "BSF"        },
  114.     { REG_5 + BIT_3 + 1,            0x600,    "BTFSC"        },
  115.     { REG_5 + BIT_3 + 1,            0x700,    "BTFSS"        },
  116.     { ADDR_9 + 2,                0x643,    "BZ"        },
  117.     { ADDR_8 + 1,                0x900,    "CALL"        },
  118.     { NONE + 1,                0x403,    "CLRC"        },
  119.     { NONE + 1,                0x423,    "CLRDC"        },
  120.     { REG_5 + 1,                0x060,    "CLRF"        },
  121.     { NONE + 1,                0x040,    "CLRW"        },
  122.     { NONE + 1,                0x004,    "CLRWDT"    },
  123.     { NONE + 1,                0x443,    "CLRZ"        },
  124.     { REG_5 + DIR + 1,            0x240,    "COMF"        },
  125.     { REG_5 + DIR + 1,            0x0c0,    "DECF"        },
  126.     { REG_5 + DIR + 1,            0x2c0,    "DECFSZ"    },
  127.     { PSEUDO,                DS,    "DS"        },
  128.     { PSEUDO,                DW,    "DW"        },
  129.     { PSEUDO + ISIF,            ELSE,    "ELSE"        },
  130.     { PSEUDO,                END,    "END"        },
  131.     { PSEUDO + ISIF,            ENDIF,    "ENDIF"        },
  132.     { PSEUDO,                EQU,    "EQU"        },
  133.     { ADDR_9 + 1,                0xa00,    "GOTO"        },
  134.     { PSEUDO + ISIF,            IF,    "IF"        },
  135.     { REG_5 + DIR + 1,            0x280,    "INCF"        },
  136.     { REG_5 + DIR + 1,            0x3c0,    "INCFSZ"    },
  137.     { PSEUDO,                INCL,    "INCL"        },
  138.     { IMMED_8 + 1,                0xd00,    "IORLW"        },
  139.     { REG_5 + DIR + 1,            0x100,    "IORWF"        },
  140.     { REG_5 + DIR + 1,            0x200,    "MOVF"        },
  141.     { REG_5 + 1,                0x200,    "MOVFW"        },
  142.     { IMMED_8 + 1,                0xc00,    "MOVLW"        },
  143.     { REG_5 + 1,                0x020,    "MOVWF"        },
  144.     { NONE + 1,                0x000,    "NOP"        },
  145.     { NONE + 1,                0x002,    "OPTION"    },
  146.     { PSEUDO,                ORG,    "ORG"        },
  147.     { PSEUDO,                PAGE,    "PAGE"        },
  148.     { IMMED_8 + 1,                0x800,    "RETLW"        },
  149.     { REG_5 + DIR + 1,            0x340,    "RLF"        },
  150.     { REG_5 + DIR + 1,            0x300,    "RRF"        },
  151.     { PSEUDO,                SET,    "SET"        },
  152.     { NONE + 1,                0x503,    "SETC"        },
  153.     { NONE + 1,                0x523,    "SETDC"        },
  154.     { NONE + 1,                0x543,    "SETZ"        },
  155.     { NONE + 1,                0x703,    "SKPC"        },
  156.     { NONE + 1,                0x723,    "SKPDC"        },
  157.     { NONE + 1,                0x603,    "SKPNC"        },
  158.     { NONE + 1,                0x623,    "SKPNDC"    },
  159.     { NONE + 1,                0x643,    "SKPNZ"        },
  160.     { NONE + 1,                0x743,    "SKPZ"        },
  161.     { NONE + 1,                0x003,    "SLEEP"        },
  162.     { REG_5 + DIR + 1,            0x080,    "SUBWF"        },
  163.     { REG_5 + DIR + 1,            0x380,    "SWAPF"        },
  164.     { PSEUDO,                TITLE,    "TITLE"        },
  165.     { TRIS_REG + 1,                0x000,    "TRIS"        },
  166.     { REG_5 + 1,                0x220,    "TSTF"        },
  167.     { IMMED_8 + 1,                0xf00,    "XORLW"        },
  168.     { REG_5 + DIR + 1,            0x180,    "XORWF"        }
  169.     };
  170.  
  171.     return bsearch(opctbl,opctbl + (sizeof(opctbl) / sizeof(OPCODE)),nam);
  172. }
  173.  
  174. /*  Operator table search routine.  This routine pats down the        */
  175. /*  operator table for a given operator and returns either a pointer    */
  176. /*  to it or NULL if the opcode doesn't exist.                */
  177.  
  178. OPCODE *find_operator(nam)
  179. char *nam;
  180. {
  181.     OPCODE *bsearch();
  182.  
  183.     static OPCODE oprtbl[] = {
  184.     { VAL,                        0,    "@FSR"    },
  185.     { BINARY + LOG1  + OPR,                AND,    "AND"    },
  186.     { BINARY + RELAT + OPR,                '=',    "EQ"    },
  187.     { VAL,                        1,    "F"    },
  188.     { VAL,                        0,    "F0"    },
  189.     { VAL,                        1,    "F1"    },
  190.     { VAL,                        10,    "F10"    },
  191.     { VAL,                        11,    "F11"    },
  192.     { VAL,                        12,    "F12"    },
  193.     { VAL,                        13,    "F13"    },
  194.     { VAL,                        14,    "F14"    },
  195.     { VAL,                        15,    "F15"    },
  196.     { VAL,                        16,    "F16"    },
  197.     { VAL,                        17,    "F17"    },
  198.     { VAL,                        18,    "F18"    },
  199.     { VAL,                        19,    "F19"    },
  200.     { VAL,                        2,    "F2"    },
  201.     { VAL,                        20,    "F20"    },
  202.     { VAL,                        21,    "F21"    },
  203.     { VAL,                        22,    "F22"    },
  204.     { VAL,                        23,    "F23"    },
  205.     { VAL,                        24,    "F24"    },
  206.     { VAL,                        25,    "F25"    },
  207.     { VAL,                        26,    "F26"    },
  208.     { VAL,                        27,    "F27"    },
  209.     { VAL,                        28,    "F28"    },
  210.     { VAL,                        29,    "F29"    },
  211.     { VAL,                        3,    "F3"    },
  212.     { VAL,                        30,    "F30"    },
  213.     { VAL,                        31,    "F31"    },
  214.     { VAL,                        4,    "F4"    },
  215.     { VAL,                        5,    "F5"    },
  216.     { VAL,                        6,    "F6"    },
  217.     { VAL,                        7,    "F7"    },
  218.     { VAL,                        8,    "F8"    },
  219.     { VAL,                        9,    "F9"    },
  220.     { VAL,                        4,    "FSR"    },
  221.     { BINARY + RELAT + OPR,                GE,    "GE"    },
  222.     { BINARY + RELAT + OPR,                '>',    "GT"    },
  223.     { UNARY  + UOP3  + OPR,                HIGH,    "HIGH"    },
  224.     { BINARY + RELAT + OPR,                LE,    "LE"    },
  225.     { UNARY  + UOP3  + OPR,                LOW,    "LOW"    },
  226.     { BINARY + RELAT + OPR,                '<',    "LT"    },
  227.     { BINARY + MULT  + OPR,                MOD,    "MOD"    },
  228.     { BINARY + RELAT + OPR,                NE,    "NE"    },
  229.     { UNARY  + UOP2  + OPR,                NOT,    "NOT"    },
  230.     { BINARY + LOG2  + OPR,                OR,    "OR"    },
  231.     { VAL,                        2,    "PC"    },
  232.     { VAL,                        5,    "PORTA"    },
  233.     { VAL,                        6,    "PORTB"    },
  234.     { VAL,                        7,    "PORTC"    },
  235.     { VAL,                        1,    "RTCC"    },
  236.     { BINARY + MULT  + OPR,                SHL,    "SHL"    },
  237.     { BINARY + MULT  + OPR,                SHR,    "SHR"    },
  238.     { VAL,                        3,    "SW"    },
  239.     { VAL,                        0,    "W"    },
  240.     { BINARY + LOG2  + OPR,                XOR,    "XOR"    }
  241.     };
  242.  
  243.     return bsearch(oprtbl,oprtbl + (sizeof(oprtbl) / sizeof(OPCODE)),nam);
  244. }
  245.  
  246. static OPCODE *bsearch(lo,hi,nam)
  247. OPCODE *lo, *hi;
  248. char *nam;
  249. {
  250.     SCRATCH int i;
  251.     SCRATCH OPCODE *chk;
  252.  
  253.     for (;;) {
  254.     chk = lo + (hi - lo) / 2;
  255.     if (!(i = ustrcmp(chk -> oname,nam))) return chk;
  256.     if (chk == lo) return NULL;
  257.     if (i < 0) lo = chk;
  258.     else hi = chk;
  259.     }
  260. }
  261.  
  262. static int ustrcmp(s,t)
  263. char *s, *t;
  264. {
  265.     SCRATCH int i;
  266.  
  267.     while (!(i = toupper(*s++) - toupper(*t)) && *t++);
  268.     return i;
  269. }
  270.  
  271. /*  Buffer storage for line listing routine.  This allows the listing    */
  272. /*  output routines to do all operations without the main routine    */
  273. /*  having to fool with it.                        */
  274.  
  275. static FILE *list = NULL;
  276.  
  277. /*  Listing file open routine.  If a listing file is already open, a    */
  278. /*  warning occurs.  If the listing file doesn't open correctly, a    */
  279. /*